<template>
  <div>
     <tw-chart
        :dataset="schoolDataLine.dataset"
        :chartOption="schoolDataLine.chartOption"
        :chartDataMaps="schoolDataLine.chartDataMaps"
        height="260px"
      ></tw-chart>
  </div>
</template>
<script>
import echarts from 'echarts'
import TwChart from '@/components/TwChart.vue'
export default {
  components: {TwChart},
  name: 'tw-linebar',
  props: {
    source: {
      type: String,
      default: 'school'
    },
    degree: {
      type: String,
      default: '1'
    },
    tjnf: {
      type: String,
      default: '2019'
    }
  },
  data () {
    return {
      schoolDataLine: {
        dataset: [],
        chartOption: {
          color: ['#00e2fe', '#ffe666', '#1890ff'],
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              label: {
                backgroundColor: '#6a7985'
              }
            }
          },
          legend: {
            data: ['现有学校数量', '需要学校数量'],
            top: '2%',
            textStyle: {
              fontSize: 16
            },
            borderRadius: 5
          },
          grid: {
            top: '25%',
            left: '5%',
            right: '8%',
            bottom: '4%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: [],
              boundaryGap: true,
              axisLabel: {
                interval: 0,
                fontSize: 16
              }
            }],
          yAxis: [
            {
              type: 'value',
              splitNumber: 3,
              nameTextStyle: {
                color: '#94aad4',
                fontSize: 16
              },
              axisLabel: {
                fontSize: 16
              }
            },
            {
              type: 'value',
              splitNumber: 3,
              nameTextStyle: {
                color: '#94aad4',
                fontSize: 16
              },
              axisLabel: {
                fontSize: 16
              }
            }
          ],
          series: []
        },
        chartDataMaps: []
      }
    }
  },
  created () {
    this.initData()
  },
  watch: {
    degree () {
      this.initData()
    }
  },
  methods: {
    initData () {
      let vm = this
      let jyjd = '小学'

      vm.$axios.get('/static/weishidataview/jsondata/graduategongshu.json', {
        baseURL: ''
      }).then((resp) => {
        if (vm.source === 'school') {
          let yAxis = vm.schoolDataLine.chartOption.yAxis
          yAxis[0].name = '学校(所)'
          yAxis[1].name = '超额学校(所)'

          if (vm.degree === '1') {
            // vm.schoolDataLine.dataset = resp.data.school.dataset
            jyjd = '小学'
            vm.app_jxszhjy_fwnlfx_xx(jyjd)
          } else if (vm.degree === '2') {
            jyjd = '初中'
            // vm.schoolDataLine.dataset = resp.data.school.dataset1
            vm.app_jxszhjy_fwnlfx_xx(jyjd)
          } else {
            jyjd = '高中'
            // vm.schoolDataLine.dataset = resp.data.school.dataset2
            vm.app_jxszhjy_fwnlfx_xx(jyjd)
          }
          vm.schoolDataLine.chartDataMaps = resp.data.school.chartDataMaps.map(item => {
            return {
              ...item,
              type: item.name === '超额学校数量' ? 'bar' : 'line',
              yAxisIndex: item.name === '超额学校数量' ? 1 : 0,
              barMaxWidth: 15,
              smooth: true,
              lineStyle: {
                type: item.dataKey === 'grade3' || item.dataKey === 'grade4'
                  ? 'dot'
                  : 'solid'
              },
              itemStyle: {
                barBorderRadius: [5, 5, 0, 0]
              }
            }
          })
        } else if (vm.source === 'house') {
          let yAxis = vm.schoolDataLine.chartOption.yAxis
          yAxis[0].name = '面积总数(㎡)'
          yAxis[1].name = '生均面积(㎡)'
          if (vm.degree === '1') {
            // vm.schoolDataLine.dataset = resp.data.house.dataset
            jyjd = '小学'
            vm.app_jxszhjy_fwnlfx_jxjfzyfmj(jyjd)
          } else if (vm.degree === '2') {
            // vm.schoolDataLine.dataset = resp.data.house.dataset1
            jyjd = '初中'
            vm.app_jxszhjy_fwnlfx_jxjfzyfmj(jyjd)
          } else {
            // vm.schoolDataLine.dataset = resp.data.house.dataset2
            jyjd = '高中'
            vm.app_jxszhjy_fwnlfx_jxjfzyfmj(jyjd)
          }
          vm.schoolDataLine.chartDataMaps = resp.data.house.chartDataMaps.map(item => {
            return {
              ...item,
              type: item.name === '生均面积' ? 'bar' : 'line',
              yAxisIndex: item.name === '生均面积' ? 1 : 0,
              barMaxWidth: 15,
              smooth: true,
              lineStyle: {
                type: item.dataKey === 'grade3' || item.dataKey === 'grade4'
                  ? 'dot'
                  : 'solid'
              },
              markLine: item.name === '生均面积' ? {
                symbol: 'none',
                data: [{
                  name: '生均面积',
                  yAxis: 55,
                  label: {
                    show: 'true',
                    position: 'middle',
                    formatter: '最低标准55M'
                  },
                  lineStyle: {
                    normal: {
                      width: 2,
                      color: '#FF5D1D'
                    }
                  }
                }]
              } : {},
              itemStyle: {
                barBorderRadius: [5, 5, 0, 0]
              }
            }
          })
        } else if (vm.source === 'student') {
          let yAxis = vm.schoolDataLine.chartOption.yAxis
          yAxis[0].name = '人数(人)'
          vm.schoolDataLine.chartOption.yAxis = [
            {
              name: '人数(人)',
              type: 'value',
              splitNumber: 3,
              axisLabel: {
                fontSize: 16
              },
              nameTextStyle: {
                color: '#94aad4',
                fontSize: 16
              }
            }
          ]
          vm.schoolDataLine.chartOption.series = [{
            areaStyle: {color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
              offset: 0,
              color: 'rgba(212,137,141,0)'
            }, {
              offset: 1,
              color: 'rgba(212,137,141,0.35)'
            }])}
          }]
          if (vm.degree === '1') {
            // vm.schoolDataLine.dataset = resp.data.student.dataset
            jyjd = '小学'
            vm.app_jxszhjy_fwnlfx_xs(jyjd)
          } else if (vm.degree === '2') {
            // vm.schoolDataLine.dataset = resp.data.student.dataset1
            jyjd = '初中'
            vm.app_jxszhjy_fwnlfx_xs(jyjd)
          } else {
            // vm.schoolDataLine.dataset = resp.data.student.dataset2
            jyjd = '高中'
            vm.app_jxszhjy_fwnlfx_xs(jyjd)
          }
          let idxstu = parseInt(vm.degree) - 1
          vm.schoolDataLine.chartDataMaps = resp.data.student.chartDataMaps[idxstu].map(item => {
            return {
              ...item,
              type: 'line',
              smooth: true,
              lineStyle: {
                type: item.dataKey === 'grade3' || item.dataKey === 'grade4' || item.dataKey === 'grade6'
                  ? 'dot'
                  : 'solid'
              }
            }
          })
        } else {
          let yAxis = vm.schoolDataLine.chartOption.yAxis
          yAxis[0].name = '人数(人)'
          yAxis[1].name = '每百名学生教师数'
          if (vm.degree === '1') {
            // vm.schoolDataLine.dataset = resp.data.teacher.dataset
            jyjd = '小学'
            vm.app_jxszhjy_fwnlfx_js(jyjd)
          } else if (vm.degree === '2') {
            // vm.schoolDataLine.dataset = resp.data.teacher.dataset1
            jyjd = '初中'
            vm.app_jxszhjy_fwnlfx_js(jyjd)
          } else {
            // vm.schoolDataLine.dataset = resp.data.teacher.dataset2
            jyjd = '高中'
            vm.app_jxszhjy_fwnlfx_js(jyjd)
          }
          vm.schoolDataLine.chartDataMaps = resp.data.teacher.chartDataMaps.map(item => {
            return {
              ...item,
              type: item.name === '每百名学生教师数' ? 'bar' : 'line',
              yAxisIndex: item.name === '每百名学生教师数' ? 1 : 0,
              barMaxWidth: 15,
              smooth: true,
              lineStyle: {
                type: item.dataKey === 'grade3' || item.dataKey === 'grade4'
                  ? 'dot'
                  : 'solid'
              },
              itemStyle: {
                barBorderRadius: [5, 5, 0, 0]
              },
              markLine: item.name === '每百名学生教师数' ? {
                symbol: 'none',
                data: [{
                  name: '每百名学生教师数',
                  yAxis: 1.5,
                  label: {
                    show: 'true',
                    position: 'middle',
                    formatter: '最低标准人数'
                  },
                  lineStyle: {
                    normal: {
                      width: 2,
                      color: '#FF5D1D'
                    }
                  }
                }]
              } : {}
            }
          })
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 服务能力分析_学校统计
    async app_jxszhjy_fwnlfx_xx (queryData) {
      let vm = this
      // let quary = {
      //   jyjd: queryData
      // }
      let quary = { 'conditions': [], 'fields': [] }
      let timeSectionValue = { 'fieldName': 'jyjd', 'fieldValues': [queryData], 'operator': 'IN', 'prepender': 'AND' }
      quary.conditions.push(timeSectionValue)

      let dataset = []
      await vm.$axios
        .post('tw-cloud-datatheme-server/themedata/app_jxszhjy_fwnlfx_xx', quary)
        .then(
          (response) => {
            if (response.data.pageInfo && response.data.pageInfo.list.length > 0) {
              let toolTips = vm.schoolDataLine.chartOption.tooltip
              toolTips.formatter = (params) => {
                let res = params[0].name + '<br />'
                if (params[0].name < 2020) {
                  res += params[0].marker + params[0].seriesName + ': ' + params[0].value + '所' + '<br />'
                  res += params[1].marker + params[1].seriesName + ': ' + params[1].value + '所' + '<br />'
                  res += params[2].marker + params[2].seriesName + ': ' + params[2].value + '所'
                } else if (params[0].name >= 2020) {
                  res += params[2].marker + params[2].seriesName + ': ' + params[2].value + '所' + '<br />'
                  res += params[3].marker + params[3].seriesName + ': ' + params[3].value + '所' + '<br />'
                  res += params[4].marker + params[4].seriesName + ': ' + params[4].value + '所'
                }
                return res
              }
              vm.tjnf = '2019'
              response.data.pageInfo.list.forEach(item => {
                if (queryData === item.jyjd) {
                  if (item.tjnf < vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade1: item.yyxxsl,
                      grade2: item.xyxxsl,
                      grade5: item.cexxsl
                    })
                  } else if (item.tjnf === vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade1: item.yyxxsl,
                      grade2: item.xyxxsl,
                      grade3: item.xyxxsl,
                      grade4: item.yyxxsl,
                      grade5: item.cexxsl
                    })
                  } else if (item.tjnf > vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade3: item.xyxxsl,
                      grade4: item.yyxxsl,
                      grade5: item.cexxsl
                    })
                  }
                }
              })
            }
            vm.schoolDataLine.dataset = dataset
          },
          (response) => {
            console.log('error')
          }
        )
    },
    // 服务能力分析_教学及辅助用房面积统计
    app_jxszhjy_fwnlfx_jxjfzyfmj (queryData) {
      let vm = this
      // let quary = {
      //   jyjd: queryData
      // }
      let quary = { 'conditions': [], 'fields': [] }
      let timeSectionValue = { 'fieldName': 'jyjd', 'fieldValues': [queryData], 'operator': 'IN', 'prepender': 'AND' }
      quary.conditions.push(timeSectionValue)
      let dataset = []
      vm.$axios
        .post('tw-cloud-datatheme-server/themedata/app_jxszhjy_fwnlfx_jxjfzyfmj', quary)
        .then(
          (response) => {
            if (response.data.pageInfo && response.data.pageInfo.list.length > 0) {
              vm.tjnf = '2019'
              let toolTips = vm.schoolDataLine.chartOption.tooltip
              toolTips.formatter = (params) => {
                let res = params[0].name + '<br />'
                if (params[0].name < 2020) {
                  res += params[0].marker + params[0].seriesName + ': ' + params[0].value + '㎡' + '<br />'
                  res += params[1].marker + params[1].seriesName + ': ' + params[1].value + '㎡' + '<br />'
                  res += params[2].marker + params[2].seriesName + ': ' + params[2].value + '㎡'
                } else if (params[0].name >= 2020) {
                  res += params[2].marker + params[2].seriesName + ': ' + params[2].value + '㎡' + '<br />'
                  res += params[3].marker + params[3].seriesName + ': ' + params[3].value + '㎡' + '<br />'
                  res += params[4].marker + params[4].seriesName + ': ' + params[4].value + '㎡'
                }
                return res
              }
              response.data.pageInfo.list.forEach(item => {
                if (queryData === item.jyjd) {
                  if (item.tjnf < vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade1: item.sjjfzyfmj,
                      grade2: item.xymjsl,
                      grade5: item.sjmj
                    })
                  } else if (item.tjnf === vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade1: item.sjjfzyfmj,
                      grade2: item.xymjsl,
                      grade3: item.xymjsl,
                      grade4: item.sjjfzyfmj,
                      grade5: item.sjmj
                    })
                  } else if (item.tjnf > vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade3: item.xymjsl,
                      grade4: item.sjjfzyfmj,
                      grade5: item.sjmj
                    })
                  }
                }
              })
            }
            vm.schoolDataLine.dataset = dataset
          },
          (response) => {
            console.log('error')
          }
        )
    },
    // 服务能力分析_教师统计
    app_jxszhjy_fwnlfx_js (queryData) {
      let vm = this
      // let quary = {
      //   jyjd: queryData
      // }
      let quary = { 'conditions': [], 'fields': [] }
      let timeSectionValue = { 'fieldName': 'jyjd', 'fieldValues': [queryData], 'operator': 'IN', 'prepender': 'AND' }
      quary.conditions.push(timeSectionValue)
      let dataset = []
      vm.$axios
        .post('tw-cloud-datatheme-server/themedata/app_jxszhjy_fwnlfx_js', quary)
        .then(
          (response) => {
            if (response.data.pageInfo && response.data.pageInfo.list.length > 0) {
              vm.tjnf = '2019'
              let toolTips = vm.schoolDataLine.chartOption.tooltip
              toolTips.formatter = (params) => {
                let res = params[0].name + '<br />'
                if (params[0].name < 2020) {
                  res += params[0].marker + params[0].seriesName + ': ' + params[0].value + '人' + '<br />'
                  res += params[1].marker + params[1].seriesName + ': ' + params[1].value + '人' + '<br />'
                  res += params[2].marker + params[2].seriesName + ': ' + params[2].value + '人'
                } else if (params[0].name >= 2020) {
                  res += params[2].marker + params[2].seriesName + ': ' + params[2].value + '人' + '<br />'
                  res += params[3].marker + params[3].seriesName + ': ' + params[3].value + '人' + '<br />'
                  res += params[4].marker + params[4].seriesName + ': ' + params[4].value + '人'
                }
                return res
              }
              response.data.pageInfo.list.forEach(item => {
                if (queryData === item.jyjd) {
                  if (item.tjnf < vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade1: item.zgjssl,
                      grade2: item.xyzrjssl,
                      grade5: item.mbmxsjss
                    })
                  } else if (item.tjnf === vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade1: item.zgjssl,
                      grade2: item.xyzrjssl,
                      grade3: item.xyzrjssl,
                      grade4: item.zgjssl,
                      grade5: item.mbmxsjss
                    })
                  } else if (item.tjnf > vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade3: item.xyzrjssl,
                      grade4: item.zgjssl,
                      grade5: item.mbmxsjss
                    })
                  }
                }
              })
            }
            vm.schoolDataLine.dataset = dataset
          },
          (response) => {
            console.log('error')
          }
        )
    },
    // 服务能力分析_学生统计
    app_jxszhjy_fwnlfx_xs (queryData) {
      let vm = this
      // let quary = {
      //   jyjd: queryData
      // }
      let quary = { 'conditions': [], 'fields': [] }
      let timeSectionValue = { 'fieldName': 'jyjd', 'fieldValues': [queryData], 'operator': 'IN', 'prepender': 'AND' }
      quary.conditions.push(timeSectionValue)
      let dataset = []
      vm.$axios
        .post('tw-cloud-datatheme-server/themedata/app_jxszhjy_fwnlfx_xs', quary)
        .then(
          (response) => {
            if (response.data.pageInfo && response.data.pageInfo.list.length > 0) {
              vm.tjnf = '2019'

              let toolTips = vm.schoolDataLine.chartOption.tooltip
              toolTips.formatter = (params) => {
                let res = params[0].name + '<br />'
                if (params[0].name < 2020) {
                  res += params[0].marker + params[0].seriesName + ': ' + params[0].value + '人'
                } else if (params[0].name >= 2020) {
                  res += params[1].marker + params[1].seriesName + ': ' + params[1].value + '人'
                }
                return res
              }
              response.data.pageInfo.list.forEach(item => {
                if (queryData === item.jyjd) {
                  if (item.tjnf < vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade1: item.xsrs
                    })
                  } else if (item.tjnf === vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade1: item.xsrs,
                      grade3: item.xsrs
                    })
                  } else if (item.tjnf > vm.tjnf) {
                    dataset.push({
                      name: item.tjnf,
                      grade3: item.xsrs
                    })
                  }
                }
              })
            }
            vm.schoolDataLine.dataset = dataset
          },
          (response) => {
            console.log('error')
          }
        )
    }
  }
}
</script>
